﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using IPTV.DBUtility;
using IPTV.module;
using System.Data;

/// <summary>
/// Summary description for GradeDao
/// </summary>
namespace IPTV.dao
{
    public class CommonDao
    {
        public CommonDao()
        {

        }

        public int addUserInfo(UserDto user , MySqlTransaction transcation)
        {
            string sql = "insert into me_tgsg_user  "
                        + " values ( @uuid , @userId , @userIp , @registertime , @type , @deleteflag )";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@uuid" , MySqlDbType.VarChar, 36 ), 
                    new MySqlParameter( "@userId" , MySqlDbType.VarChar, 36 ), 
                    new MySqlParameter( "@userIp" , MySqlDbType.VarChar, 20 ), 
                    new MySqlParameter( "@registertime", MySqlDbType.DateTime ), 
                    new MySqlParameter( "@type", MySqlDbType.Int32 , 1 ), 
                    new MySqlParameter( "@deleteflag", MySqlDbType.Int32 , 1 )
                };

            parms[0].Value = System.Guid.NewGuid().ToString();
            parms[1].Value = user.UserId;
            parms[2].Value = user.UserIp;
            parms[3].Value = user.Registertime;
            parms[4].Value = user.Type;
            parms[5].Value = user.Deleteflag;

            int result = MySQLHelper.ExecuteNonQuery(transcation, CommandType.Text, sql, parms);

            return result;
             
        }

        public int getUserById(string userId)
        {
            String sql = " SELECT  count(*) FROM me_tgsg_user WHERE userId = @userId ";

             MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@userId" , MySqlDbType.VarChar, 36 )
                };

            parms[0].Value = userId;

            DataRow dr = MySQLHelper.ExecuteDataRow(MySQLHelper.CONNECTION_STRING, sql, parms);
            int result = Convert.ToInt32(dr[0].ToString());
            return result;
        }


        public int addVisitRecord(VisitRecordDto visitRecord , MySqlTransaction transcation)
        {
            string sql = "insert into me_tgsg_visitrecord  "
                        + " values ( @uuid , @url , @idtype , @entityid , @grade , @subject , @userid , @userip ,@createdtime )";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@uuid" , MySqlDbType.VarChar, 36 ), 
                    new MySqlParameter( "@url" , MySqlDbType.VarChar, 1000 ), 
                    new MySqlParameter( "@idtype" , MySqlDbType.VarChar, 20 ), 
                    new MySqlParameter( "@entityid", MySqlDbType.VarChar, 36 ), 
                    new MySqlParameter( "@grade", MySqlDbType.VarChar, 36 ), 
                    new MySqlParameter( "@subject", MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@userid", MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@userip", MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@createdtime", MySqlDbType.DateTime )
                };

            parms[0].Value = System.Guid.NewGuid().ToString();
            parms[1].Value = visitRecord.Url;
            parms[2].Value = visitRecord.IdType;
            parms[3].Value = visitRecord.EntityId;
            parms[4].Value = visitRecord.Grade;
            parms[5].Value = visitRecord.Subject;
            parms[6].Value = visitRecord.Userid;
            parms[7].Value = visitRecord.Userip;
            parms[8].Value = visitRecord.CreatedTime;

            int result = MySQLHelper.ExecuteNonQuery(transcation, CommandType.Text, sql, parms);

            return result;

        }
    }
}